package com.haole.ecommercepay.transactions.service.impl;

import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.haole.ecommercepay.common.BaseParam;
import com.haole.ecommercepay.common.HttpUtils;
import com.haole.ecommercepay.transactions.entity.QueryTransactionsParam;
import com.haole.ecommercepay.transactions.entity.close.CloseTransactionsParam;
import com.haole.ecommercepay.transactions.entity.jsapi.JsapiParam;
import com.haole.ecommercepay.transactions.entity.jsapi.PayParam;
import com.haole.ecommercepay.transactions.service.TransactionsService;
import com.haole.ecommercepay.transactions.url.TransactionsUrl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * @author haole
 */
@Service("transactionsService")
public class TransactionsServiceImpl implements TransactionsService {

    private static final Logger logger = LoggerFactory.getLogger(TransactionsServiceImpl.class);

    /**
     * 合单下单-JS支付
     * @param param
     * @return
     */
    @Override
    public JSONObject jsapi(JsapiParam param, BaseParam baseParam) {
        String requestParam = JSON.toJSONString(param);
        logger.info("jsapi param:{}",requestParam);
        HttpResponse response = HttpUtils.v3HttpExecute("POST", TransactionsUrl.JSAPI,
                baseParam.getAppName(),baseParam.getMchId(),baseParam.getMchSerialNo(),
                baseParam.getWechatSerialNo(),requestParam,baseParam.getMchPrivateKeyPath(),null);
        logger.info("jsapi result:{}",response);
        return HttpUtils.httpResult(response,baseParam.getWechatPubKeyPath());
    }
    /**
     * 合单下单-APP支付
     * @param param
     * @return
     */
    @Override
    public JSONObject appPay(JsapiParam param, BaseParam baseParam) {
        String requestParam = JSON.toJSONString(param);
        logger.info("appPay param:{}",requestParam);
        HttpResponse response = HttpUtils.v3HttpExecute("POST", TransactionsUrl.APP_API,
                baseParam.getAppName(),baseParam.getMchId(),baseParam.getMchSerialNo(),
                baseParam.getWechatSerialNo(),requestParam,baseParam.getMchPrivateKeyPath(),null);
        logger.info("appPay result:{}",response);
        return HttpUtils.httpResult(response,baseParam.getWechatPubKeyPath());
    }
    /**
     * 合单下单-H5支付
     * @param param
     * @return
     */
    @Override
    public JSONObject h5Pay(PayParam param, BaseParam baseParam) {
        String requestParam = JSON.toJSONString(param);
        logger.info("h5Pay param:{}",requestParam);
        HttpResponse response = HttpUtils.v3HttpExecute("POST", TransactionsUrl.H5_API,
                baseParam.getAppName(),baseParam.getMchId(),baseParam.getMchSerialNo(),
                baseParam.getWechatSerialNo(),requestParam,baseParam.getMchPrivateKeyPath(),null);
        logger.info("h5Pay result:{}",response);
        return HttpUtils.httpResult(response,baseParam.getWechatPubKeyPath());
    }
    /**
     * 合单下单-Native支付
     * @param param
     * @return
     */
    @Override
    public JSONObject nativePay(PayParam param, BaseParam baseParam) {
        String requestParam = JSON.toJSONString(param);
        logger.info("nativePay param:{}",requestParam);
        HttpResponse response = HttpUtils.v3HttpExecute("POST", TransactionsUrl.NATIVE_API,
                baseParam.getAppName(),baseParam.getMchId(),baseParam.getMchSerialNo(),
                baseParam.getWechatSerialNo(),requestParam,baseParam.getMchPrivateKeyPath(),null);
        logger.info("nativePay result:{}",response);
        return HttpUtils.httpResult(response,baseParam.getWechatPubKeyPath());
    }
    /**
     * 合单下单查询订单
     * @param param
     * @return
     */
    @Override
    public JSONObject queryTransactions(QueryTransactionsParam param) {
        logger.info("queryTransactions param:{}",param);
        HttpResponse response = HttpUtils.v3HttpExecute("GET", String.format(TransactionsUrl.QUERY_TRANSACTIONS,param.getCombine_out_trade_no()),
                param.getAppName(),param.getMchId(),param.getMchSerialNo(),
                param.getWechatSerialNo(),"",param.getMchPrivateKeyPath(),null);
        logger.info("queryTransactions result:{}",response);
        return HttpUtils.httpResult(response,param.getWechatPubKeyPath());
    }
    /**
     * 合单关闭订单
     * @param param
     * @param combineOutTradeNo 合单支付商户总订单号
     * @return
     */
    @Override
    public JSONObject closeTransactions(CloseTransactionsParam param, String combineOutTradeNo) {
        String requestParam = JSON.toJSONString(param);
        logger.info("closeTransactions param:{}",requestParam);
        HttpResponse response = HttpUtils.v3HttpExecute("POST", String.format(TransactionsUrl.CLOSE_TRANSACTIONS,combineOutTradeNo),
                param.getAppName(),param.getMchId(),param.getMchSerialNo(),
                param.getWechatSerialNo(),requestParam,param.getMchPrivateKeyPath(),null);
        logger.info("closeTransactions result:{}",response);
        return HttpUtils.httpResult(response,param.getWechatPubKeyPath());
    }
}
